// pages/login/login.js
const app = getApp()
var host = app.globalData.host;
var self = null;

Page({
  data: {
    themeColor: ''
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  getUserInfo(e) {
    console.log(e);
    var userInfo = e.detail.userInfo;
    var nickName = userInfo.nickName;
    var avatarUrl = userInfo.avatarUrl;
    var gender = userInfo.gender; //性别 0：未知、1：男、2：女
    var province = userInfo.province;
    var city = userInfo.city;
    var country = userInfo.country;
    wx.login({
      success: function (res) {
        var code = res.code;
        wx.request({
          url: host + '/yq/auth',
          data: {
            code: code,
            nickName: nickName,
            avatarUrl: avatarUrl,
            gender: gender,
            province: province,
            country: country
          },
          success: function (res) {
            console.log(res);
            var userInfo = res.data.info.user;
            var unionId = res.data.info.unionId;
            var openId = res.data.info.openId;
            var token = res.data.info.token;
            wx.setStorageSync("openId", openId);
            wx.setStorageSync("token", token);
            app.globalData.userInfo = userInfo;

            if (wx.getStorageSync("path")) {
              wx.reLaunch({
                url: '/' + wx.getStorageSync("path")
              })
              wx.setStorageSync("path", null);
            } else {
              wx.reLaunch({
                url: '/pages/index/index'
              })
            }
          }
        })
      },
      fail(errMsg) {
        console.log(errMsg)
      }
    })
  }
})
